package haizhi.fruitmall.service.impl;

import com.github.pagehelper.PageHelper;
import haizhi.fruitmall.dao.mapper.GoodsPictureMapper;
import haizhi.fruitmall.exception.ResultException;
import haizhi.fruitmall.pojo.form.GoodsPictureForm;
import haizhi.fruitmall.pojo.po.GoodsPicture;
import haizhi.fruitmall.pojo.po.GoodsPictureExample;
import haizhi.fruitmall.service.GoodsPictureService;
import haizhi.fruitmall.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Date: 2017/12/25 9:20
 * @Author: Richard Zane
 */

@Service
@Transactional
public class GoodsPictureServiceImpl implements GoodsPictureService {

    @Autowired
    private GoodsPictureMapper mapper;

    @Override
    public void insert(GoodsPictureForm form) {
        GoodsPicture record = new GoodsPicture();
        BeanUtil.copyBean(form,record);
        record.setPictureId(BeanUtil.longId());
        mapper.insertSelective(record);
    }

    @Override
    public void update(GoodsPictureForm form){
        GoodsPicture record = this.getone(form.getPictureId());
        if(BeanUtil.isNull(record)){ throw new ResultException("记录不存在！"); }
        BeanUtil.copyBean(form,record);
        record.setUpdateTime(BeanUtil.newTime());
        mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void delete(Long id) {
        if(BeanUtil.isNull(this.getone(id))){ throw new ResultException("记录不存在！"); }
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public GoodsPicture getone(Long id) {
        if(BeanUtil.isNull(id)){ throw new ResultException("编号为空！");}
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<GoodsPicture> getall(GoodsPictureForm form) {
        GoodsPictureExample example = new GoodsPictureExample();
        GoodsPictureExample.Criteria criteria = example.createCriteria();
        if(BeanUtil.noNull(form.getPictureType())){
            criteria.andPictureTypeEqualTo(form.getPictureType());
        }
        if(BeanUtil.noNull(form.getGoodsId())){
            criteria.andGoodsIdEqualTo(form.getGoodsId());
        }
        if(BeanUtil.noNull(form.getPageNum()) && BeanUtil.noNull(form.getPageSize())){
            PageHelper.startPage(form.getPageNum(),form.getPageSize());
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        List<GoodsPicture> goodsPictureList =  mapper.selectByExample(example);
        for(GoodsPicture goodsPicture : goodsPictureList){
            String picturePath = BeanUtil.getPath(request)+goodsPicture.getPicturePath();
            goodsPicture.setPicturePath(picturePath);
        }
        return goodsPictureList;
    }

}
